package cool.zhoujie.pageselect;

import android.util.Log;
import android.view.View;
import android.widget.Toast;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import java.util.ArrayList;
import java.util.List;

public class MainViewModel extends ViewModel {

    MutableLiveData<List<Selector>> mList = new MutableLiveData<>();

    private List<Selector> list = new ArrayList<>();

    private ItemAdapter mAdapter;

    //输入的结果
    public String input = "";

    //选中的值
    int selected_number = 1;

    public MutableLiveData<Boolean> isGone = new MutableLiveData<>();

    public MainViewModel() {
        isGone.setValue(true);
    }

    void setAdapetr(ItemAdapter adapetr) {
        mAdapter = adapetr;
    }

    //根据输入结果，更新list
    public void click(View view) {
        list.clear();

        isGone.setValue(false);

        //判断输入的数量，并刷新显示list
        int count = Integer.parseInt(input);

        if (count == 0) {
            Toast.makeText(view.getContext(), "页面数量为0", Toast.LENGTH_SHORT).show();
        } else if (count <= 9) {
            for (int i = 1; i <= count; i++) {
                Selector selector = new Selector(String.valueOf(i), i, false);
                list.add(selector);
            }
        } else {
            for (int i = 1; i <= 9; i++) {
                if (i == 8) {
                    Selector selector = new Selector("...", i, false);
                    list.add(selector);
                } else if (i == 9) {
                    Selector selector = new Selector(String.valueOf(count), i, false);
                    list.add(selector);
                } else {
                    Selector selector = new Selector(String.valueOf(i), i, false);
                    list.add(selector);
                }
            }
        }
        mList.setValue(list);
    }

    //点击listItem，更新list的数字
    public void clickItem(Selector selector) {
        mAdapter.notifyDataSetChanged();
        //输入的数量
        int count = Integer.parseInt(input);

        //点击选中的数字
        //选中的数字
        if (!selector.number.equals("...")) {
            selected_number = Integer.parseInt(selector.number);
        }

        Log.d("选中的数字值为", selector.number);

        int result = selected_number - 3;

        if (count > 9) {
            if (selected_number >= 5 && selected_number < count - 4) {
                for (int i = 2; i < list.size()-2; i++) {
                    result++;
                    list.get(i).number = String.valueOf(result);
                }
                list.get(1).number = "...";
                list.get(7).number = "...";
                mList.setValue(list);
            } else if (selected_number >= count - 4) {
                for (int i = list.size() - 2; i > 1; i--) {
                    count--;
                    list.get(i).number = String.valueOf(count);
                }
                mList.setValue(list);
            }
        }
    }

    public void clickLeft() {
        if (selected_number > 1) {
            selected_number--;
        }
        refreshNumber();
        Log.d("当前选中的数字为：", String.valueOf(selected_number));
        mAdapter.notifyDataSetChanged();
    }

    public void clickRight() {
        int i = Integer.parseInt(input);
        if (selected_number < i) {
            selected_number++;
        }
        refreshNumber();
        Log.d("当前选中的数字为：", String.valueOf(selected_number));
        mAdapter.notifyDataSetChanged();
    }

    private void refreshNumber() {
        //输入的数量
        int count = Integer.parseInt(input);

        //点击选中的数字
        //选中的数字

        int result = selected_number - 3;

        if (count > 9) {
            if (selected_number >= 5 && selected_number < count - 4) {
                for (int i = 2; i < list.size()-2; i++) {
                    result++;
                    list.get(i).number = String.valueOf(result);
                }
                list.get(1).number = "...";
                list.get(7).number = "...";
                mList.setValue(list);
            } else if (selected_number >= count - 4) {
                for (int i = list.size() - 2; i > 1; i--) {
                    count--;
                    list.get(i).number = String.valueOf(count);
                }
                mList.setValue(list);
            }
        }
    }

}
